<?php

use DiDom\Document;
use Requests;

class IndexAction extends frontendAction {
    
    public function index() {
    
    	 
    	/*****首页广告***/
    	$ad= M('ad');
    	$ads= $ad->field('url,content,desc')->where('board_id=1 and status=1')->limit('0,5')->order('ordid desc')->select();
        $this->assign('ad',$ads);
        /*****首页广告end******/
        
        /****热卖推荐*****/
        $wherehot=array('hot'=>1);
        $hot=M('goods')->where($wherehot)->limit('0,6')->order('id desc')->select();
		foreach($hot as $key=>$v){
			$item_cate = M('item_cate')->where(array('id'=>$v['cid']))->find();
			if($item_cate)
				$hot[$key]['cate_name'] = $item_cate['name'];
		}
        $this->assign('hots',$hot);
         /****热卖推荐 END*****/

        //客服电话
        $config = M('config')->where('name="customer"')->find();
        if($config) {
            $this->assign('tel', $config['content']);
        }
      
//        $this->assign('tel',C('pin_kefu_tel'));
        $this->_config_seo();
        $this->display();
    }
	
	public function search(){
        $config = M('config')->where('name="searchTip"')->find();
        if($config) {
            $tips = explode(' ', $config['content']);
            $this->assign('tips', $tips);
        }
		$this->display();
	}
	
	public function rule(){
		$this->display();
	}
	
	private function jisuan($num, $count, $typestr){
		$utype = M('usertype');
		
		$utwhere = array(
			'type' => array('like', "$typestr%"),
			'tiaojian0' => array('elt', $num),
			'tiaojian1' => array('egt', $num),
		);
		$u_list = $utype->where($utwhere)->find();
		if($u_list){
			$bili = $u_list['bili'];
		}else{
			$bili = 0;
		}
		return ($count * $bili);
	}

    public function calc() {
        if($this->visitor->is_login) {
            $uid = $this->visitor->info['id'];
            $Order = new OrderModel();
            $rebate = $Order->getUserRebateMonth($uid);
            $this->assign('award', $rebate);
        }

        $this->display();
    }

    public function calc_data() {
        $cated_goods = array();

        $cates = array('1' => '汽车膜', '2' => '建筑膜');
        foreach($cates as $id => $cate) {
            $sub_cates = M('item_cate')->where("pid=" . $id)->field('id')->select();
            $cid = array();
            foreach($sub_cates  as $sub) {
                $cid[] = $sub['id'];
            }

            $where_goods['cid'] = array('in', join(',', $cid));
            $goods = M('goods')->where($where_goods)->select();
            $cated_goods[] = array('cid' => $id, 'name' => $cate, 'goods' => $goods);
        }

        $this->ajaxReturn(200, 'ok', $cated_goods);
    }

    /**
     * 计算用户奖励
     * @param $uid
     * @param $range
     * @return array
     */
	protected function calcUserAward($uid, $range) {
        $orders = M('item_orders');
        $map = array(
            'uid' => $uid,
            'status' => 4,
            'type'  => 1,
            'cate'  => '汽车膜',
            'done_time' => array('between', implode(',', $range) )
        );


        $result = array();

        $c_result = $orders->where($map)->field('sum(`count`) as c, sum(zhifue) as money')->find();
        if(empty($c_result['c'])) {
            $result[1] = array('award'=>0, 'count' => 0, 'money' => 0);
        } else {
            $result[1] = array(
                'award' => $this->_calc(1, $c_result['c'], $c_result['money']),
                'count' =>  $c_result['c'],
                'money' =>  $c_result['money']
            );
        }

        $map['cate'] = '建筑膜';
        $p_result = $orders->where($map)->field('sum(`count`) as c, sum(zhifue) as money')->find();
        if(empty($p_result['c'])) {
            $result[2] = array('award'=>0, 'count' => 0, 'money' => 0);
        } else {
            $result[2] = array(
                'award' => $this->_calc(1, $p_result['c'], $p_result['money']),
                'count' =>  $p_result['c'],
                'money' =>  $p_result['money']
            );
        }

        return $result;
    }
	
    public function getItem($where = array(), $show_cate = false)
    {
    	$where_init = array('status'=>'1');
        $where =array_merge($where_init, $where);
    	$item=M('item')->where($where)->select();
		foreach($item as $key=>$v){
			$cate_name = D('item_cate')->get_name($v['cate_id']);
			$item[$key]['cate_name'] = $cate_name;
		}
    	return $item;
    }
    
    
    public function ajaxLogin()
    {
    	
        $user_name=$_POST['user_name'];
       $password=$_POST['password'];
	   $f=$_POST['f'];
       
       $user=M('user');
	   if($f!='p')
		   $password = md5($password);
       $users= $user->where("username='".$user_name."' and password='".$password."'")->find();
	   
	   if(is_array($users))
       {
    	$data = array('status'=>1);
    	$_SESSION['user_info']=$users;
       }else {
       	$data = array('status'=>0);
       }
    	
    	echo json_encode($data);
    	exit;
    }
    public function ajaxRegister()
    {
    	$username=$_POST['user_name'];
    	$user=M('user');
    	$count=$user->where("username='".$username."'")->find();
    	if(is_array($count))
    	{
        echo 'false';
       // echo json_encode(array('user_nameData'=>true));
    	}else 
    	{
    		echo 'true';
        //echo json_encode(array('user_nameData'=>true));
    	}
    	
    
    }

    public function calculate($cid, $num, $money) {
        $this->ajaxReturn(1,'',rebate($cid, $num, $money));
    }

    public function bankArea($account) {
        $result = Requests::post("http://www.guabu.com/bank/", array(), array('cardid' => $account));
        $dom = new Document($result->body);
        $result = null;//释放内存

        function getBankArea ($dom) {
            $value = $dom->find('td.STYLE2');
            if(count($value) < 4) {
                return false;
            }

            $area = trim($value[3]->text());
            if(strlen($area) < 3) return false;

            return $area;
        }

        $area = getBankArea($dom);
        if($area) {
            $this->ajaxReturn(200, 'ok', $area);
        } else {
            $this->ajaxReturn(400, 'failed');
        }
    }
}